package com.ynding.cloud.common.util;

/**
 * <p> </p>
 *
 * @author ynding
 * @version 2021/5/27
 **/
public class TimeUtils {

    /**
     * 获取两个时间节点之间的月份列表
     **/
    public static List<String> getMonthBetween(Date minDate, Date maxDate) {
        ArrayList<String> result = new ArrayList<String>();
        try {
            //格式化为年月
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");

            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(minDate);
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(maxDate);
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * localDate 转化成 Date
     * @param localDate
     * @return
     */
    public static Date LocalDate2Date(LocalDate localDate){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }
}
